#include "hwt.h"
#include <math.h>
#ifdef DEBUG
#include <stdio.h>
#endif

//integer Haar wavelet transform
void hwt(int *h, unsigned char *l, unsigned char x, unsigned char y) {
  int x_ = (int)x, y_ = (int)y;
  *h = x_ - y_;
  *l = (unsigned char)((x_ + y_) / 2); //implicit floor
}

//inverse integer Haar wavelet transform
void ihwt(unsigned char *x, unsigned char *y, int h, unsigned char l) {
  int l_ = (int)l;
  *x = (unsigned char)(l_ + (int)floor((float)(h+1) / 2));
  *y = (unsigned char)(l_ - (int)floor((float)h / 2));
}

void hwt_bitmap(int **d, unsigned char **a, unsigned char **b, unsigned int h, unsigned int w) {
  int i, j;
  for(i=0; i<h; ++i) {
    for(j=0; j<w; j+=2) {
      hwt(&(d[i][j/2]), &(a[i][j/2]), b[i][j], b[i][j+1]);
    }
  }
}

void ihwt_bitmap(unsigned char **b, int **d, unsigned char **a, unsigned int h, unsigned int w) {
  int i, j;
  for(i=0; i<h; ++i)
    for(j=0; j<w; j+=2)
      ihwt(&(b[i][j]), &(b[i][j+1]), d[i][j/2], a[i][j/2]);
}
